import request from "@/utils/request";
import type { ChatSession, ChatMessage, DeleteChatMessagesResponse ,} from "@/types/chat";

export type { ChatSession, ChatMessage };

// 获取聊天会话列表
export const getChatSessions = (params?: { title?: string }) => {
  return request.post<ChatSession[]>("/api/client/chat/sessions/list", { params });
};

// 获取会话详情
export const getChatSessionDetail = (chatId: string) => {
  return request.post<{ session: ChatSession; messages: ChatMessage[] }>(
    "/api/client/chat/sessions/detail",
    { chatId }
  );
};

// 创建新会话
export const createChatSession = (agentId?: string) => {
  return request.post<ChatSession>("/api/client/chat/sessions/create", { agentId });
};

// 删除会话
export const deleteChatSession = (chatId: string) => {
  return request.post("/api/client/chat/sessions/delete", { chatId });
};

// 清空会话
export const clearChatSession = (chatId: string) => {
  return request.post("/api/client/chat/sessions/clear", { chatId });
};

// 更新会话
export const updateChatSession = (chatId: string, title: string) => {
  return request.post(`/api/client/chat/sessions/update`, { chatId, title });
};


/**
 * 删除指定消息之后的所有聊天记录
 */
export const deleteChatMessagesAfter = async (
  chatId: string,
  messageId?: string,
  options?: {
    messageContent?: string;
    messageRole?: 'user' | 'assistant';
    messageTimestamp?: string;
  }
): Promise<DeleteChatMessagesResponse> => {
  return await request.post<DeleteChatMessagesResponse>('/api/client/chat/messages/delete-after', {
    chatId,
    messageId,
    messageContent: options?.messageContent,
    messageRole: options?.messageRole,
    messageTimestamp: options?.messageTimestamp
  });
};